package org.jeecg.modules.engineering.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.engineering.entity.EquipCollectTrigger;
import org.jeecg.modules.engineering.entity.EquipEventSort;
import org.jeecg.modules.engineering.mapper.EquipCollectTriggerMapper;
import org.jeecg.modules.engineering.service.IEquipCollectTriggerService;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 设备采集触发事件规则表
 * @Author: jeecg-boot
 * @Date:   2019-08-20
 * @Version: V1.0
 */
@Service
public class EquipCollectTriggerServiceImpl extends ServiceImpl<EquipCollectTriggerMapper, EquipCollectTrigger> implements IEquipCollectTriggerService {
	
	@Autowired
	private EquipCollectTriggerMapper equipCollectTriggerMapper;

	@Autowired
	private RedisUtil redisUtil;
	
	@Override
	public List<EquipCollectTrigger> selectByMainId(String mainId) {
		return equipCollectTriggerMapper.selectByMainId(mainId);
	}

	@Override
	public void setToRedis(String itemId){
		List<EquipCollectTrigger> triggers = equipCollectTriggerMapper.selectByItem(itemId);
		redisUtil.set(CacheConstant.REDIS_TABLES+"equip_collect_item_detail:child:"+itemId, JSONArray.toJSONString(triggers));
	}

	@Override
	public void removeFromRedis(String itemId) {
		redisUtil.del(CacheConstant.REDIS_TABLES+"equip_collect_item_detail:child:"+itemId);
	}

	@Override
	public List<EquipCollectTrigger> getFromRedisById(String itemId) {
		return JSONArray.parseArray((String)redisUtil.get(CacheConstant.REDIS_TABLES+"equip_collect_item_detail:child:"+itemId),EquipCollectTrigger.class);
	}

	@Override
	public boolean checkHaveByEventIds(List<String> ids) {
		long count = equipCollectTriggerMapper.selectCountByEventIds(ids,ids.size());
		return count > 0;
	}
}
